package com.example.ejemplos_enahoandroidutil;

import java.util.ArrayList;
import java.util.List;

import util.android.AndroidUtil;
import util.android.adapters.ItemSpinnerAdapter;
import util.android.widgets.SelectOneSpinnerView;
import util.android.widgets.StringSpinner3View;
import util.form.respuesta.listener.OnCambioValor1Listener;
import android.app.Activity;
//import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;

public class MainActivity extends Activity 
//extends ActionBarActivity 
{
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		try {
			ejemploSpinnerAutocompletar1();
			ejemploSpinnerAutocompletar2();			
			ejemploSpinner3();
		} catch (Exception e) {
			AndroidUtil.mostrarExcepcionEnToast(getBaseContext(), e);
		}
		
	}

	private void ejemploSpinner3() throws Exception {
		final StringSpinner3View ss3 = (StringSpinner3View) findViewById(R.id.spn3);
		ss3.getSpinner1().agregar("100","Ana Meza");
		ss3.getSpinner1().agregar("200","Luis Prieto");
		ss3.getSpinner1().agregar("300","Luis Prieto");
		ss3.getSpinner1().agregar("400","Luis Prieto");
		ss3.getSpinner1().agregar("500","Luis Prieto");
		
		ss3.getSpinner1().setOnPostCambioValorListener(new OnCambioValor1Listener() {
			@Override
			public void onCambioValor(String valor) throws Exception {
				ss3.getSpinner2().limpiar();
				if(valor.equals("200")) {					
					ss3.getSpinner2().agregar("210","Ana Meza");
					ss3.getSpinner2().agregar("220","Luis Prieto");
					ss3.getSpinner2().agregar("230","Luis Prieto");
					ss3.getSpinner2().agregar("240","Luis Prieto");
					ss3.getSpinner2().agregar("250","Luis Prieto");	
				}
				if(valor.equals("300")) {
					ss3.getSpinner2().agregar("310","Jorge Perez");
					ss3.getSpinner2().agregar("320","Jorge Rodriguez");
					ss3.getSpinner2().agregar("330","Jorge Rodriguez");
					ss3.getSpinner2().agregar("340","Jorge Rodriguez");
				}
			}
		});
				
		ss3.getSpinner2().setOnPostCambioValorListener(new OnCambioValor1Listener() {
			
			@Override
			public void onCambioValor(String valor) throws Exception {
				ss3.getSpinner3().limpiar();
				if(valor!=null && valor.equals("220")) {
					ss3.getSpinner3().agregar("221","Jorge Tello");
					ss3.getSpinner3().agregar("222","Julio Perez");
					ss3.getSpinner3().agregar("223","Julio Perez");
				}
				if(valor!=null && valor.equals("320")) {
					ss3.getSpinner3().agregar("321","Jorge Chavez");
					ss3.getSpinner3().agregar("322","Jorge Chavez");
					ss3.getSpinner3().agregar("323","Jorge Chavez");
				}
			}
		});
				
		ss3.getInput().setOnPostCambioValorListener(new OnCambioValor1Listener() {
			
			@Override
			public void onCambioValor(String valor) throws Exception {
				ss3.getSpinner1().setValor("200");
				ss3.getSpinner2().setValor("220");
				ss3.getSpinner3().setValor("222");
			}
		});
		
		ss3.getInput().setValor("123");
	}
	
	private void ejemploSpinnerAutocompletar1() throws Exception {
		SelectOneSpinnerView sp = (SelectOneSpinnerView) findViewById(R.id.spn);
		sp.getInput().setAutocompletar(true);				
		sp.getInput().agregar("1","Ana Meza");
		sp.getInput().agregar("2","Luis Prieto");
		sp.getInput().agregar("3","Freddy Gonzales");
		sp.getInput().agregar("4","Jorge Perez");
		sp.getInput().agregar("5","Jorge Rodriguez");
		sp.getInput().agregar("6","Jorge Tello");
		sp.getInput().agregar("7","Julio Perez");
		sp.getInput().agregarOtro("8","Otro","otro");
	}
	
	private void ejemploSpinnerAutocompletar2() {
		List<Persona> lista = new ArrayList<Persona>();
		lista.add(new Persona("100","Luis Morales"));
		lista.add(new Persona("200","Pedro Morales"));
		lista.add(new Persona("300","Ana Lopez"));
		lista.add(new Persona("400","Fernando Cruz"));
		
		ItemSpinnerAdapter<Persona> adp = new ItemSpinnerAdapter<Persona>(getBaseContext(),lista) {			
			@Override
			public String getLabel(Persona obj) {				
				return obj.getDni()+" "+obj.getNombre();
			}
			
			@Override
			public String getKey(Persona obj) {
				return obj.getDni();
			}
		};
		
		((SelectOneSpinnerView) findViewById(R.id.spn2))
		.getInput()
		.setAutocompletar(true)
		.setAdapter(adp)
		//.setEtiqueta("Con autocompletar")				
		;
				
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
